﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>This sample shows how to use the</title>
<style type="text/css">
.style1 {
	color: #0000FF;
}
.style2 {
	line-height: 115%;
	font-size: 10.0pt;
	font-family: Consolas;
	margin-left: .2in;
	margin-right: 0in;
	margin-top: 0in;
	margin-bottom: 10.0pt;
}
.style3 {
	color: #FF0000;
}
.style4 {
	color: #0D540D;
}
.style5 {
	color: #000000;
}
</style>
</head>

<body>

<p>This sample shows how to use the C# 4.0 features called <em>COM Interop</em>,
<em>omitting <strong>ref</strong>, indexed properties</em> and<em> Named and Optional Parameters</em> to create a C# 
application that communicates with Microsoft Office. C# developers have traditionally had 
to write relatively verbose code in order to access Microsoft Office applications such as 
Word or Excel.&nbsp; New C# 4.0 features make it much simpler to call Office 
APIs.</p>
<p>Consider this declaration for a Microsoft Office method used in this sample:</p>
<p>void PasteSpecial(<span class="style1">ref object </span>IconIndex =
<span class="style1">null</span>, <span class="style1">ref object</span> Link =
<span class="style1">null</span>, <BR>
	&nbsp;&nbsp;&nbsp;
	<span class="style1">ref object </span>Placement =
<span class="style1">null</span>, <span class="style1">ref object </span>
DisplayAsIcon = <span class="style1">null</span>,<span class="style1"> </span><BR>
	<span class="style1">&nbsp;&nbsp;&nbsp; ref object</span> DataType =
<span class="style1">null</span>, <span class="style1">ref object</span> 
IconFileName = <span class="style1">null</span>, <BR>
	<span class="style1">&nbsp;&nbsp;&nbsp; ref object</span> IconLabel =
<span class="style1">null</span>);</p>

<p>As you can see, this method takes a fairly large number of parameters. In C#, 
developers have traditionally had to fill out each parameter, even though the 
developers of this call had intended to simplify its use by supporting optional 
parameters. In C# 4.0, the new support for named and optional parameters allows 
the developer to specify only the parameters of interest, and to take default 
values for the other parameters:</p>
<p>word.Selection.PasteSpecial(Link: <span class="style1">true</span>, 
DisplayAsIcon: <span class="style1">true</span>);</p>
<p>In the call to th<strong>e PasteSpecial</strong> method the <strong>Link</strong> 
and <strong>DisplayAsIcon</strong> parameters are explicitly named, and set to 
the value <span class="style5"><strong>true</strong></span>. All the other parameters default to values specified internally 
by the developers of the Office API, as shown in the above signature.</p>
<p>You can create your own calls that support named and optional paramters. 
Consider this example:</p>
<p class="style2"><span class="style1">public void</span> M(<span class="style1">int</span> 
x, <span class="style1">int</span> y = 5, <span class="style1">int</span> z = 7) { }</p>
<p>In this method, the parameters y and z are assigned default values. Calls to 
this method might look like this:</p>
<p>M(<span class="style3">1</span>, <span class="style3">2</span>,
<span class="style3">3</span>); <span class="style4">// ordinary call of M</span><br />
M(<span class="style3">1</span>, 2);<span class="style4"> // omitting z – 
equivalent to M(1, 2, 7)</span><br />
M(<span class="style3">1</span>);&nbsp; <span class="style4">// omitting both y 
and z – equivalent to M(1, 5, 7)</span><br />
M(<span class="style3">1</span>, z:<span class="style3"> 3</span>);
<span class="style4">// passing z by name</span><br />
M(x: <span class="style3">1</span>, z:<span class="style3"> 3</span>);
<span class="style4">// passing both x and z by name</span><br />
M(z:<span class="style3"> 3</span>, x:<span class="style3"> 1</span>);
<span class="style4">// reversing the order of arguments</span></p>
<p>A new dynamic feature in C# 4.0 makes Office much easier for C# developers 
to use. Types used in Office are now presented to C# developers as if they were 
declared with the type <strong>dynamic</strong>. Here is the traditionally way to set a Cell 
property:</p>
<p class="style2">((Excel.Range)excel.Cells[1, 1]).Value2 = &quot;ID&quot;;</p>
<p>In C# 4.0 developers can now write code that looks like this:</p>
<p class="style2">X1.Cells[<span class="style3">1</span>, <span class="style3">1</span>].Value =<span class="style3"> &quot;ID&quot;</span>;</p>
<p>A feature called <em>Index Properties</em> allows us to simplify the call further, so 
that it looks like this:</p>
<p class="style2">xl.Cells[1, 1] = &quot;ID&quot;;</p>
<p>A final feature of interest to Office Developers is called No-PIA. Primary 
Interop Assemblies are generated from COM interfaces and provide helpful type 
support at design time. At runtime, however, they increase the size of your 
program, and can cause versioning issues. The No-PIA feature allows you to 
continue to use PIAs at design but omit them at runtime. The C# compiler will 
bake the small part of the PIA that a program actually uses directly into its 
assembly. You will no longer need to include PIA&#39;s in the distribution of your 
programs.</p>

</body>

</html>
